package com.dq.Week1.day05.homeWork2;

import java.util.Random;
import java.util.Scanner;

public class Demo2 {
    public static void main(String[] args) throws InterruptedException {
        System.out.println("-----欢迎进入千锋用户管理系统-----");
        System.out.println();
        System.out.println();
        System.out.println();
        System.out.println();
        Scanner scanner = new Scanner(System.in);
        StudentUtil studentUtil = new StudentUtil();
        while(true)
        {

            System.out.println("请选择你要的功能:1.注册\t2.登录\t3.退出");
            String number = scanner.next();
           switch (number)
           {
               case "1":
                   System.out.println("请输入用户名：");
                   String username1 = scanner.next();
                   System.out.println("请输入密码：");
                   String password1 = scanner.next();
                   if(studentUtil.register(username1,password1))
                   {
                       System.out.println("注册成功");
                   }else{
                       System.out.println("注册失败");
                   }
                   break;
               case "2":
                   System.out.println("请输入用户名：");
                   String username2 = scanner.next();
                   System.out.println("请输入密码：");
                   String password2 = scanner.next();
                   String code = Code();
                   System.out.println("请输入验证码："+code);
                   String yzm = scanner.next();
                   if(code.equals(yzm))
                   {
                       System.out.println("验证成功");
                       if(studentUtil.login(username2,password2))
                       {
                           System.out.println("登陆成功");

                       }else{
                           System.out.println("登陆失败");
                       }

                   }else{
                       System.out.println("验证码错误");
                   }
                   break;
               case "3":
                   System.out.println("欢迎下次使用");
                   return;
               default:
                   System.out.println("输入有误，请重新输入");
                   break;
           }

        }
    }

    public static String Code()
    {
        String[] number = {"1","2","3","4","5","6","7","8"};
        Random random = new Random();
        String code = "";

        for (int i = 0; i < 4; i++) {
            int index = random.nextInt(number.length);//[0,8)
            code = code + number[index];
        }
        return code;
    }
}
